package org.fireflow2.designer.eclipse.properties.tab;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;

//import commonproperties.model.FileModel;

public class ExtendAttributePropertySection extends AbstractPropertySection {

	// private FileModel fileModel = null;
	TableViewer tableViewer = null;
	AbstractEditPart editPart = null;
	ModelWrapper modelWrapper = null;
	
	@Override
	public void createControls(final Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		parent.setLayout(new GridLayout(2,false));
		
		
		tableViewer = new TableViewer(parent, SWT.MULTI | SWT.BORDER
				| SWT.V_SCROLL| SWT.FULL_SELECTION);
		Table table = tableViewer.getTable();
		
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		String[] columnNames = new String[] { "Key", "Value" };
		int[] columnAlignments = new int[] { SWT.LEFT, SWT.CENTER, };
		for (int i = 0; i < columnNames.length; i++) {
			TableColumn tableColumn = new TableColumn(table,
					columnAlignments[i]);
			tableColumn.setText(columnNames[i]);
			tableColumn.setWidth(200);
		}

		GridData gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace=true;
		gridData.grabExcessVerticalSpace=true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment=SWT.FILL;
		gridData.heightHint = 65;
		table.setLayoutData(gridData);

		tableViewer.setLabelProvider(new ExtendAttributeTableLabelProvider());

		tableViewer.setContentProvider(new ArrayContentProvider());

		//AddButton
		Button okButton = new Button(parent, SWT.PUSH);
		okButton.setText("Add");
		gridData = new GridData();
		gridData.horizontalAlignment=SWT.RIGHT;
		okButton.setLayoutData(gridData);
		okButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				addExtendAttribute();
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				addExtendAttribute();
			}

			private void addExtendAttribute() {
				ExtendAttributeDialog dialog = new ExtendAttributeDialog(parent.getShell());
				dialog.open();
				if (dialog.getReturnCode()==Dialog.OK){
					ExtendAttribute extendAttribute = dialog.getExtendAttribute();
					//modelWrapper.addExtendAttribute(extendAttribute.getKey(), extendAttribute.getValue());
					refresh();
					
				}
			}

		});		
		
		Button deleteButton = getWidgetFactory().createButton(parent, "Delete",
				SWT.PUSH);
		gridData = new GridData();
		gridData.horizontalAlignment=SWT.LEFT;
		deleteButton.setLayoutData(gridData);
		deleteButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				deleteExtendAttribute();
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				deleteExtendAttribute();
			}

			private void deleteExtendAttribute() {
				StructuredSelection selection = (StructuredSelection) tableViewer
						.getSelection();
				List extendAttributeList = selection.toList();
				for (int i=0;extendAttributeList!=null && i<extendAttributeList.size();i++){
					ExtendAttribute extendAttribute = (ExtendAttribute)extendAttributeList.get(i);
					//modelWrapper.deleteExtendAttribute(extendAttribute.getKey());
				}
				refresh();
			}

		});




	}

	@Override
	public void refresh() {
		super.refresh();
		if (this.modelWrapper.getWorkflowModelElement()==null)return;
		Map extendAttributeMap = null;//this.modelWrapper.getExtendedAttributes();
		if (extendAttributeMap==null) return;
		List extendAttributeList = new ArrayList();
		Iterator keyIterator = extendAttributeMap.keySet().iterator();
		while(keyIterator.hasNext()){
			String key = (String)keyIterator.next();
			if (key.startsWith("FIRE_FLOW.")){
				continue;
			}
			String value = (String)extendAttributeMap.get(key);
			ExtendAttribute extendAtt = new ExtendAttribute(key,value);
			extendAttributeList.add(extendAtt);
		}
		tableViewer.setInput(extendAttributeList);
	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart) {
			editPart = (AbstractEditPart) obj;
			modelWrapper = (ModelWrapper) editPart.getModel();
		}
	}

}

class ExtendAttributeTableLabelProvider extends LabelProvider implements
		ITableLabelProvider {
	public Image getColumnImage(Object element, int index) {
		return null;
	}

	public String getColumnText(Object element, int index) {
		ExtendAttribute extendAttribute = (ExtendAttribute) element;
		switch (index) {
		case 0:
			return extendAttribute.getKey();
		case 1:
			return extendAttribute.getValue();
		default:
			return "unknown " + index;
		}
	}
}

class ExtendAttribute {
	String key = null;
	String value = null;

	public ExtendAttribute(String key, String value) {
		this.key = key;
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}
}

class ExtendAttributeDialog extends Dialog{
	Text keyText;
	Text valueText;
	ExtendAttribute extendAttribute = null;
	public ExtendAttributeDialog(Shell shell){
		super(shell);
	}
	protected Control createDialogArea(Composite parent){
//		datafieldsWrapper.setLayout(new FillLayout());
		Composite panel = (Composite) super.createDialogArea(parent);

		panel.setLayout(new GridLayout(2,false));
		
		Label label1 = new Label(panel,SWT.NONE);
		label1.setText("Key:");
		keyText = new Text(panel,SWT.SINGLE|SWT.BORDER);
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		keyText.setLayoutData(gridData);
		
		Label label2 = new Label(panel,SWT.NONE);
		label2.setText("Value:");
		valueText = new Text(panel,SWT.SINGLE|SWT.BORDER);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		valueText.setLayoutData(gridData);
		
		return panel;
		
	}
	
	protected  boolean 	isResizable() {
		return true;
	}
	
	protected  void 	okPressed() {

		if (keyText.getText()==null || keyText.getText().trim().equals("")){
			return;
		}
		String value = valueText.getText();
		if (value==null) value="";
		value = value.trim();
		extendAttribute = new ExtendAttribute(keyText.getText().trim(),value);
		super.okPressed();
	}
	
	public ExtendAttribute getExtendAttribute(){
		return this.extendAttribute;
	}
}
